﻿using System.Collections.Generic;
using System.Windows;
using Microsoft.Windows.Controls;

namespace WDCF
{
    /// <summary>
    /// DataGrid Cell Formatter
    /// </summary>
    public class DataGridConditionalCellFormatter : DataGridConditionalFormatter
    {
        private DataGridColumn affectedColumn;

        /// <summary>
        /// Initializes a new instance of the <see cref="DataGridConditionalCellFormatter"/> class.
        /// </summary>
        /// <param name="dataGrid"></param>
        /// <param name="condition"></param>
        public DataGridConditionalCellFormatter(
            DataGrid dataGrid,
            Condition condition)
            : base(dataGrid, new List<Condition> {condition})
        {
            
        }

        /// <summary>
        /// Gets the column that will be affected by the conditional style
        /// </summary>
        private DataGridColumn AffectedColumn
        {
            get
            {
                if (affectedColumn == null && Grid.Columns != null)
                {
                    foreach (var dataGridColumn in Grid.Columns)
                    {
                        //check the header first
                        if (dataGridColumn.Header != null)
                        {
                            if (dataGridColumn.Header.Equals(Conditions[0].PropertyName))
                            {
                                affectedColumn = dataGridColumn;
                                break;
                            }
                        }
                        else
                        {
                            //if header is not set, try to use the SortMemberPath property
                            if (!string.IsNullOrEmpty(dataGridColumn.SortMemberPath)
                                && dataGridColumn.SortMemberPath.Equals(Conditions[0].PropertyName))
                            {
                                affectedColumn = dataGridColumn;
                                break;
                            }
                            
                            //if sort member is not set, try to use binding
                            var column = dataGridColumn as DataGridBoundColumn;
                            if(column != null)
                            {
                                if(((System.Windows.Data.Binding)column.Binding).Path.Path.Equals(Conditions[0].PropertyName))
                                {
                                    affectedColumn = dataGridColumn;
                                    break;
                                }
                            }
                        }
                        
                        
                    }
                }
                return affectedColumn;
            }
        }

        #region Overrides of DataGridConditionalFormatter

        /// <summary>
        /// Generates a <see cref="Style"/> object and applies it to the grid.
        /// Target is <see cref="DataGridCell"/>.
        /// </summary>
        public override void Apply()
        {
            var style = GetStyle<DataGridCell>();

            if(AffectedColumn != null && style != null)
            {
                AffectedColumn.CellStyle = style;
            }
        }

        /// <summary>
        /// Gets the <see cref="Style"/> object which shall be replaced with
        /// conditional formatting. Gets the cell style of the affected column.
        /// </summary>
        protected override Style TargetStyle
        {
            get
            {
                return AffectedColumn != null ? AffectedColumn.CellStyle : null;
            }
        }

        #endregion
    }
}
